package maple.util;

import java.lang.IllegalArgumentException;
import java.lang.Math;

public class Sensor extends Object
{
	public static enum Type{
						     MASTER, GATEWAY,
							 III_ACTIVEPOWER,
							 III_POWERFACTOR,
						     PHIDGET_HUMIDITY, 
							 PHIDGET_LIGHT,
							 PHIDGET_MOTION, 
							 PHIDGET_SOUND, 
							 PHIDGET_TEMPERATURE,
							 PHIDGET_VIBRATION
						   };

	/**
	 * return the String representation of the specified sensor and data or null if it is not valid
	 *
	 * @param	sensor	the specified sensor
	 * @param	data	the specified data
	 * @return	the String representation of the specified sensor and data
	 *
	 * @author	snowfoxy
	 * @since	2011-11-15
	 */
	public static String toString( final String sensor, final String data )
	{
		try
		{
			double value = -1;
			
			Sensor.Type type = Sensor.Type.valueOf( sensor );
			switch( type )
			{
				case PHIDGET_HUMIDITY:
					value = Double.parseDouble( data );
					value = ( value * 0.1906 ) - 40.2;
					break;
				case PHIDGET_MOTION: case PHIDGET_VIBRATION:
					value = Double.parseDouble( data );
					value = Math.abs( value - 500 );
					break;
				case PHIDGET_SOUND:
					value = Double.parseDouble( data );
					value = ( Math.log1p( value - 1 ) * 16.801 ) + 9.872;
					break;
				case PHIDGET_TEMPERATURE:
					value = Double.parseDouble( data );
					value = ( value * 0.22222 ) - 61.11;
					break;
				default:
					return data;
			}
			
			return Double.toString( value );
		}
		catch( IllegalArgumentException e )
		{
		}
		return null;
	}
}
